package system.student.Context;

public class LogContext {
    private static final ThreadLocal<LogContextData> context = new ThreadLocal<>();

    // 设置当前上下文
    public static void setContext(LogContextData data) {
        context.set(data);
    }

    // 清除上下文（防止内存泄漏）
    public static void clear() {
        context.remove();
    }

    // 获取当前用户ID
    public static Long getCurrentUserId() {
        return context.get() != null ? context.get().getUserId() : null;
    }

    // 获取当前用户姓名
    public static String getCurrentUserName() {
        return context.get() != null ? context.get().getUserName() : null;
    }

    // 上下文数据载体
    public static class LogContextData {
        private Long userId;
        private String userName;

        public Long getUserId() {
            return userId;
        }

        public void setUserId(Long userId) {
            this.userId = userId;
        }

        public String getUserName() {
            return userName;
        }

        public void setUserName(String userName) {
            this.userName = userName;
        }

        public LogContextData(Long userId, String userName) {
            this.userId = userId;
            this.userName = userName;
        }
    }
}